﻿namespace EasyWeb.Tool.AllInOne
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;

    static class Program
    {
        static void Main(string[] args)
        {
            using (var writer = new StreamWriter(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "resources.xml"), false, Encoding.UTF8))
            {
                var fieldTypeInfo = typeof(EWFieldDefinition).GetTypeInfo();

                foreach (var type in fieldTypeInfo.Assembly.ExportedTypes)
                {
                    var typeInfo = type.GetTypeInfo();

                    if (!fieldTypeInfo.IsAssignableFrom(typeInfo))
                    {
                        continue;
                    }

                    var name = typeInfo.Name.Substring(typeof(EWFieldDefinition).Name.Length);

                    foreach (var property in typeInfo.DeclaredProperties)
                    {
                        var attribute = property.GetCustomAttribute<DisplayAttribute>();

                        if (attribute == null)
                        {
                            continue;
                        }

                        writer.WriteLine("<data name=\"FieldDefinition_Prompt_{1}_{2}\" xml:space=\"preserve\">{0}<value />{0}</data>", Environment.NewLine, name, property.Name);
                        writer.WriteLine("<data name=\"FieldDefinition_Name_{1}_{2}\" xml:space=\"preserve\">{0}<value />{0}</data>", Environment.NewLine, name, property.Name);
                        writer.WriteLine("<data name=\"FieldDefinition_ShortName_{1}_{2}\" xml:space=\"preserve\">{0}<value />{0}</data>", Environment.NewLine, name, property.Name);
                        writer.WriteLine("<data name=\"FieldDefinition_Description_{1}_{2}\" xml:space=\"preserve\">{0}<value />{0}</data>", Environment.NewLine, name, property.Name);
                    }
                }

                writer.Flush();
            }
        }
    }
}
